package com.xtxb.app.rmq.conf;

import com.xtxb.app.rmq.util.FileServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

@Component
public class MQServer {
    private Map<String,String> servers=new HashMap<>();
    @Autowired
    private FileServer fserver;

    @PostConstruct
    public void init(){
        Object cache=fserver.loadCache("server");
        if(cache!=null) {
            servers = (Map) cache;
        }
    }

    public boolean addServer(String name,String serverAddr){
        servers.put(name,serverAddr);
        fserver.saveCache("server",servers);
        return true;
    }

    public boolean deleteServer(String name){
        fserver.saveCache("server",servers);
        return servers.remove(name)!=null;
    }

    public Map<String,String> getSevers(){
        return servers;
    }

    public String getServer(String name){
        return servers.get(name);
    }
}
